package com.ijiuchuang.jc_localization.view.adapter;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.app.Config;
import com.ijiuchuang.jc_localization.bean.lite.Map;

/**
 * Created by Veev on 2016/6/21
 *
 * @link 384506557@qq.com
 */
public class MapListItemTouchListener extends ItemTouchHelper.Callback {

    private SettsMapListRecyclerAdapter mRecyclerAdapter;
    private FrameLayout frameView;
    private FloatingActionButton fab;
    private long defaultPosition;

    public MapListItemTouchListener(SettsMapListRecyclerAdapter mRecyclerAdapter, FrameLayout frameView, FloatingActionButton fab, long defaultPosition) {
        this.mRecyclerAdapter = mRecyclerAdapter;
        this.frameView = frameView;
        this.fab = fab;
        this.defaultPosition = defaultPosition;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {

        final int position = viewHolder.getAdapterPosition();
        final Map map = mRecyclerAdapter.getData(position);
        mRecyclerAdapter.removeData(position);

        // 默认第一条不可删除
        if (map.getId() == defaultPosition) {
            mRecyclerAdapter.insertData(position, map);
            return;
        }

        Snackbar.make(frameView, R.string.snack_title_undo, Snackbar.LENGTH_SHORT)
                .setCallback(new Snackbar.Callback() {
                    @Override
                    public void onDismissed(Snackbar snackbar, int event) {
                        super.onDismissed(snackbar, event);
                        fab.show();

                        // 若Snack消失的动作不是有点击UNDO按钮触发， 则删除数据
                        if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {
                            // 默认第一条不可删除
                            if (map.getId() != 1) {
                                Config.liteOrm.delete(map);
                            }
                        }
                    }

                    @Override
                    public void onShown(Snackbar snackbar) {
                        super.onShown(snackbar);
                        fab.hide();
                    }
                })
                .setAction(R.string.snack_action_undo, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 点击了UNDO 按钮， 将数据重新插回
                        mRecyclerAdapter.insertData(position, map);
                    }
                })
                .show();
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        // 允许左右滑动
        return true;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        // 允许上下拖动
        // pink 加入拖动排序
        return true;
    }
}
